package com.briup.es.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.es.client.BookClient;
import com.briup.es.domain.bean.OrderItem;
import com.briup.es.domain.bean.Shopcar;
import com.briup.es.domain.vo.BookVO;
import com.briup.es.domain.vo.OrderInfoVO;
import com.briup.es.domain.vo.OrderItemVO;
import com.briup.es.service.OrderItemService;
import com.briup.es.mapper.OrderItemMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem>
        implements OrderItemService {

    private final BookClient bookClient;

    @Override
    public void addOrderItems(String orderId, List<Shopcar> shopcars) {
        // 将shopcars集合内容，转换成OrderItem内容集合
        List<OrderItem> orderItems = shopcars.stream()
                .map(shopcar ->
                        OrderItem.builder().orderId(orderId).bookId(shopcar.getBookId())
                                .num(shopcar.getNum()).build())
                .toList();

        // 批量添加订单项数据
        baseMapper.insert(orderItems);
    }

    @Override
    public OrderInfoVO queryInfoByOrderId(String orderId) {
        List<OrderItem> orderItems = queryItemsByOrderId(orderId);
        // 从各个订单项中获取对应的商品id
        List<Integer> bookIds = orderItems.stream().map(OrderItem::getBookId).toList();
        // 远程调用商品服务，根据bookIds查询对应的商品信息
        List<BookVO> bookVOS = bookClient.queryByIds(bookIds).getData();
        OrderInfoVO infoVO = new OrderInfoVO();
        // 遍历订单项数据，对订单总价和总数进行计算
        int totalNum = 0;
        double totalPrice = 0.0;
        for (OrderItem orderItem : orderItems) {
            totalNum += orderItem.getNum();
            // 遍历图书数据，求取图书的总价数据
            for (BookVO bookVO : bookVOS) {
                if (Objects.equals(bookVO.getId(), orderItem.getBookId())) {
                    totalPrice += bookVO.getPrice() * orderItem.getNum();
                }
            }
        }
        infoVO.setTotalNum(totalNum);
        infoVO.setTotalPrice(totalPrice);
        // 返回infoVO数据
        return infoVO;
    }

    @Override
    public List<OrderItemVO> queryByOrderId(String orderId) {
        List<OrderItem> orderItems = queryItemsByOrderId(orderId);
        // 获取订单中图书的id，获取图书信息
        // 从各个订单项中获取对应的商品id
        List<Integer> bookIds = orderItems.stream().map(OrderItem::getBookId).toList();
        // 远程调用商品服务，根据bookIds查询对应的商品信息
        List<BookVO> bookVOS = bookClient.queryByIds(bookIds).getData();

        // 如果订单项数据不为空，那么处理数据后返回
        if (!CollectionUtils.isEmpty(orderItems)) {
            return orderItems.stream().map(orderItem -> {
                for (BookVO bookVO : bookVOS) {
                    // 如果订单中图书id与查询的数据图书id一致，那么返回订单项详情数据
                    if (Objects.equals(bookVO.getId(), orderItem.getBookId())) {
                        return OrderItemVO.builder()
                                .bookId(bookVO.getId())
                                .name(bookVO.getName())
                                .cover(bookVO.getCover())
                                .price(bookVO.getPrice())
                                .num(orderItem.getNum())
                                // 每一个订单项图书的总价
                                .totalPrice(orderItem.getNum() * bookVO.getPrice())
                                .build();
                    }
                }
                return null;
            }).toList();
        }
        return null;
    }

    private List<OrderItem> queryItemsByOrderId(String orderId) {
        LambdaQueryWrapper<OrderItem> wrapper = new LambdaQueryWrapper<OrderItem>()
                .eq(OrderItem::getOrderId, orderId);
        return baseMapper.selectList(wrapper);
    }
}




